package ru.narod.amidoriya.app.sushibaramidoriya;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.R.drawable;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.net.Uri;
import android.os.Bundle;

import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Bludo extends SherlockActivity{
	 String spprisepathfull, spprisepath;
	 String namebludo, pricebludo, dostavkabludo, weightbludo;
	 String picsmallpludo, filepicsmallpludoPath, sostavbludo;
	 Zakaz sqldbc_b1, sqldbc_b2;
	 SQLiteDatabase sqldb_b1, sqldb_b2;
	 TextView textViewtrash;
	 ImageView imageViewBludo;
	protected void onCreate(Bundle savedInstanceState) {
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.bludo);
        
       // spprisepath = getIntent().getExtras().getString("spprisepath");
       // spprisepathfull = getIntent().getExtras().getString("spprisepathfull");
        
        namebludo = getIntent().getExtras().getString("namebludo");
        pricebludo = getIntent().getExtras().getString("pricebludo");
        dostavkabludo = getIntent().getExtras().getString("dostavkabludo");
        weightbludo = getIntent().getExtras().getString("weightpiudo");
        picsmallpludo = getIntent().getExtras().getString("picsmallpludo");
        filepicsmallpludoPath = getIntent().getExtras().getString("filepicsmallpludoPath");
        sostavbludo = getIntent().getExtras().getString("sostavbludo");
        textViewtrash = (TextView) findViewById(R.id.textViewtrash);
        // �������������� ��� �����
     	sqldbc_b1 = new Zakaz(this);
     	// ���� ������ � ������
     	sqldb_b1 = sqldbc_b1.getWritableDatabase();
     		
        imageViewBludo = (ImageView) findViewById(R.id.imageViewBludo);
        if (filepicsmallpludoPath == null) {
        	filepicsmallpludoPath = "";
		}
		if (filepicsmallpludoPath.equals("")) {
		} 
		else
		{
        imageViewBludo.setImageURI(Uri.parse(filepicsmallpludoPath));
		}
		///
		TextView textViewNameb = (TextView) findViewById(R.id.textViewNameb);
        if (namebludo == null) {
        	namebludo = "";
		}
		if (namebludo.equals("")) {
		} 
		else
		{
			textViewNameb.setText(namebludo);
		}
		///
		TextView textViewWeightb = (TextView) findViewById(R.id.textViewWeightb);
        if (weightbludo == null) {
        	weightbludo = "";
		}
		if (weightbludo.equals("")) {
		} 
		else
		{
			textViewWeightb.setText(weightbludo);
		}
		///
		TextView textViewPriceb = (TextView) findViewById(R.id.textViewPriceb);
        if (pricebludo == null) {
        	pricebludo = "";
		}
		if (pricebludo.equals("")) {
		} 
		else
		{
			textViewPriceb.setText(pricebludo+""+getString(R.string.valut));
		}
		///
		TextView textViewSostav = (TextView) findViewById(R.id.textViewSostav);
        if (sostavbludo == null) {
        	sostavbludo = "";
		}
		if (sostavbludo.equals("")) {
		} 
		else
		{
			textViewSostav.setText(sostavbludo);
		}
		Countbludo();
		///
		final ImageButton bludoshopminus = (ImageButton) findViewById(R.id.bludoshopminus);
		bludoshopminus.setOnClickListener(new View.OnClickListener() {
			TransitionDrawable ShopMinusTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_shop_minus);
			public void onClick(View v) {
				bludoshopminus.setImageDrawable(ShopMinusTransition);
				ShopMinusTransition.startTransition(2000);
				
				try {
					String deletesqlite = "DELETE FROM " + 
						    Zakaz.dtable + " WHERE "+ Zakaz.UID + 
						    " IN (SELECT MAX("+Zakaz.UID+") FROM " + Zakaz.dtable + 
						    " WHERE ("+ Zakaz.DNAMEDISH +" = '"+ namebludo + "') AND ("+
						    Zakaz.DPRICE +" = '"+ pricebludo + "') AND ("+ Zakaz.DVES +" = '"+ weightbludo +"'))";
						    
					sqldb_b1.execSQL(deletesqlite);
					Countbludo();
					if (Countbludo() > 0) {
						Toast.makeText(getBaseContext(), getString(R.string.trashdel), Toast.LENGTH_SHORT).show(); 
					}
				} catch (Exception e) {

				}
			}	
		});
		final ImageButton bludoshopplus = (ImageButton) findViewById(R.id.bludoshopplus);
		bludoshopplus.setOnClickListener(new View.OnClickListener() {
			TransitionDrawable ShopPlusTransition = (TransitionDrawable)getApplicationContext().getResources().getDrawable(R.drawable.transition_shop_plus);
			public void onClick(View v) {
				bludoshopplus.setImageDrawable(ShopPlusTransition);
				ShopPlusTransition.startTransition(2000);
				
				try {
					String insertsqlite = "INSERT INTO " + 
						    Zakaz.dtable + 
						     " (" + Zakaz.DNAMEDISH + "," + Zakaz.DPRICE + "," + Zakaz.DDOSTAVKA +
						     "," + Zakaz.DVES + "," + Zakaz.DPIC + "," + Zakaz.DPICFULL + "," + Zakaz.DSOSTAV +
						     ") VALUES ('" + namebludo + "','" + pricebludo + "','" + dostavkabludo + 
						     "','" + weightbludo + "','" + picsmallpludo + "','" + filepicsmallpludoPath + "','" + sostavbludo +
						     "')";
					sqldb_b1.execSQL(insertsqlite);
					Countbludo();
					Toast.makeText(getBaseContext(), getString(R.string.trashinsert), Toast.LENGTH_SHORT).show(); 
					
				} catch (Exception e) {

				}
			}	
		});
		
		

	}
	 @Override 
	  protected void onStop() { 
	    super.onStop(); 
	    sqldb_b1.close();
	    sqldbc_b1.close();
	    setResult(13);
	  } 
		@Override 
		protected void onDestroy() {
			
			super.onDestroy();
			System.gc();
			
			//  sqldb_b1.close();
			//  sqldbc_b1.close(); 
		}
		@Override
		public boolean onCreateOptionsMenu(Menu menu) { 
			getSupportMenuInflater().inflate(R.menu.pmenu, menu);
			return true;
		}
		@Override
		public boolean onPrepareOptionsMenu(Menu menu) { 

			menu.findItem(R.id.loaditem1).setEnabled(true);
			menu.findItem(R.id.pitem2).setEnabled(true);
			menu.findItem(R.id.abitem3).setEnabled(true);

			return super.onPrepareOptionsMenu(menu);
		}

		@Override
		public boolean  onOptionsItemSelected(MenuItem item) {

			switch (item.getItemId()) { 
			case android.R.id.home:
				setResult(13);
				finish();
				break;
			case R.id.loaditem1:
				GoMain();
				break; 
			case R.id.pitem2:
				//���������
				try {
					Intent Preferintent = new Intent(getApplicationContext(),PreferAmidoriya.class);
					startActivityForResult(Preferintent,0);
				} catch (Exception e) {

				}
				break; 
			case R.id.abitem3: 
				//� ���������
				try {
					Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
					startActivityForResult(intentapp,0);
				} catch (Exception e) {

				}
				break;
			}       


			return super.onOptionsItemSelected(item); 

		}
		private void GoMain() {
			AlertDialog.Builder buildergomain = new AlertDialog.Builder(this);
			buildergomain.setTitle(R.string.lt_app);		
			buildergomain.setMessage(R.string.lmmm_app);		
			buildergomain.setNegativeButton(R.string.qn_app, null);
			buildergomain.setPositiveButton(R.string.qy_app, new OnClickListener() {
				public void onClick(DialogInterface arg0, int arg1) {
					setResult(7);
					finish(); 	            	

				}
			}).create().show();	
		}
		
		private Integer Countbludo() {
			// �������������� ��� �����
			Integer ctbl =  0;
	     	sqldbc_b2 = new Zakaz(getApplicationContext());
	     	// ���� ������ � ������
	     	sqldb_b2 = sqldbc_b2.getWritableDatabase();
	     	String selbl1 = "SELECT " + Zakaz.UID + ", "+ Zakaz.DNAMEDISH + ", "
	     			 + Zakaz.DPRICE + " FROM " + Zakaz.dtable+
	     			 " WHERE ("+ Zakaz.DNAMEDISH +" = '"+ namebludo + "') AND ("+
					Zakaz.DPRICE +" = '"+ pricebludo + "') AND ("+ Zakaz.DVES +" = '"+ weightbludo +"')";
	     	
			Cursor cursorbl1 = sqldb_b2.rawQuery(selbl1, null);
			Double sbl = 0.0;
			ctbl =  cursorbl1.getCount();
			if (ctbl > 0) {
				
				while (cursorbl1.moveToNext()) {
					sbl = sbl + cursorbl1.getDouble(cursorbl1.getColumnIndex(Zakaz.DPRICE));
				}
				textViewtrash.setText(getString(R.string.trashnotnol)+" x"+Integer.valueOf(ctbl).toString()+" ("+ Double.valueOf(sbl).toString()+" "+ getString(R.string.valut)+")");
			} else
			{
				textViewtrash.setText(getString(R.string.trashnol));
			}
			cursorbl1.close();
			
	     	
			sqldb_b2.close();
		    sqldbc_b2.close();
		    //
			return ctbl;
		    
		    //
		}
		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			super.onActivityResult(requestCode, resultCode, data);

			if (resultCode == 7) {
				setResult(7);
				finish();
			}
			if (resultCode == 13) {
				setResult(13);
			}
			//����� ��� �� �������� �������� � � ���������
			if (resultCode ==  5) {
				try {
					openOptionsMenu();
				} catch (Exception e) {

				}
			}
			if (resultCode ==  9) {
				try {
					Intent Preferintent = new Intent(getApplicationContext(),PreferAmidoriya.class);
					startActivityForResult(Preferintent,0);
				} catch (Exception e) {

				}
			}
			if (resultCode ==  10) {
				try {
					Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
					startActivityForResult(intentapp,0);
				} catch (Exception e) {

				}
			}

		}
		@Override
		public void onBackPressed() {
			setResult(13);
			finish();
		}
}
